home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / tcshsrc.zoo / tcsh / tw.init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-25  |  7.1 KB  |  288 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.00/RCS/tw.init.c,v 3.1 1991/07/15 19:37:24 christos Exp $ */
  2. /*
  3.  * tw.init.c: TwENEX initializations
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "config.h"
  38. RCSID("$Id: tw.init.c,v 3.1 1991/07/15 19:37:24 christos Exp $")
  39.  
  40. #include "sh.h"
  41. #include "tw.h"
  42.  
  43. /*
  44.  * Build the command name list (and print the results).  This is a HACK until
  45.  * I can get the rehash command to include its results in the command list.
  46.  */
  47.  
  48. static int maxcommands = 0;
  49.  
  50.  
  51. void
  52. tw_clear_comm_list()
  53. {
  54.     register int i;
  55.  
  56.     have_sorted = 0;
  57.     if (numcommands != 0) {
  58. /*        for (i = 0; command_list[i]; i++) { */
  59.     for (i = 0; i < numcommands; i++) {
  60.         xfree((ptr_t) command_list[i]);
  61.         command_list[i] = NULL;
  62.     }
  63.     numcommands = 0;
  64.     }
  65. }
  66.  
  67. void
  68. tw_sort_comms()
  69. {                /* could be re-written */
  70.     register int i, forward;
  71.  
  72.     /* sort the list. */
  73.     qsort((ptr_t) command_list, (size_t) numcommands, sizeof(command_list[0]), 
  74.       (int (*) __P((const void *, const void *))) fcompare);
  75.  
  76.     /* get rid of multiple entries */
  77.     for (i = 0, forward = 0; i < numcommands - 1; i++) {
  78.     if (Strcmp(command_list[i], command_list[i + 1]) == 0) {    /* garbage */
  79.         xfree((ptr_t) command_list[i]);
  80.         forward++;        /* increase the forward ref. count */
  81.     }
  82.     else if (forward) {
  83.         command_list[i - forward] = command_list[i];
  84.     }
  85.     }
  86.     /* Fix fencepost error -- Theodore Ts'o <tytso@athena.mit.edu> */
  87.     if (forward)
  88.     command_list[i - forward] = command_list[i];
  89.     numcommands -= forward;
  90.     command_list[numcommands] = (Char *) NULL;
  91.  
  92.     have_sorted = 1;
  93. }
  94.  
  95. void
  96. tw_add_comm_name(name)        /* this is going to be called a LOT at startup */
  97.     Char   *name;
  98. {
  99.     register int length;
  100.     register long i;
  101.     register Char **ncl, **p1, **p2;
  102.  
  103.     if (maxcommands == 0) {
  104.     command_list = (Char **) xmalloc((size_t) (NUMCMDS_START *
  105.                            sizeof(command_list[0])));
  106.     maxcommands = NUMCMDS_START;
  107.     for (i = 0, p2 = command_list; i < maxcommands; i++)
  108.         *p2 = NULL;
  109.     }
  110.     else if (numcommands >= maxcommands) {
  111.     ncl = (Char **) xmalloc((size_t) ((maxcommands + NUMCMDS_INCR) *
  112.                       sizeof(command_list[0])));
  113.     for (i = 0, p1 = command_list, p2 = ncl; i < numcommands; i++)
  114.         *p2++ = *p1++;
  115.     for (; i < maxcommands + NUMCMDS_INCR; i++)
  116.         *p2++ = NULL;
  117.     xfree((ptr_t) command_list);
  118.     command_list = ncl;
  119. #ifdef COMMENT
  120.     command_list = (Char **) xrealloc(command_list, (maxcommands +
  121.                      NUMCMDS_INCR) * sizeof(command_list[0]));
  122. #endif
  123.     maxcommands += NUMCMDS_INCR;
  124.     }
  125.  
  126.     if (name[0] == '.')
  127.     return;            /* no dot commands... */
  128.     if (name[0] == '#')
  129.     return;            /* no Emacs buffer checkpoints */
  130.  
  131.     length = Strlen(name) + 1;
  132.  
  133.     if (name[length - 2] == '~')
  134.     return;            /* and no Emacs backups either */
  135.  
  136.     command_list[numcommands] = (Char *) xmalloc((size_t) (length *
  137.                                sizeof(Char)));
  138.  
  139.     copyn(command_list[numcommands], name, MAXNAMLEN);
  140.     numcommands++;
  141. }
  142.  
  143. void
  144. tw_add_builtins()
  145. {
  146.     register struct biltins *bptr;
  147.  
  148.     for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
  149.     if (bptr->bname)
  150.         tw_add_comm_name(str2short(bptr->bname));
  151.     }
  152. }
  153.  
  154. void
  155. tw_add_aliases()
  156. {
  157.     register struct varent *p;
  158.     register struct varent *c;
  159.  
  160.     p = &aliases;
  161.     for (;;) {
  162.     while (p->v_left)
  163.         p = p->v_left;
  164. x:
  165.     if (p->v_parent == 0)    /* is it the header? */
  166.         return;
  167.     if (p->v_name)
  168.         tw_add_comm_name(p->v_name);
  169.     if (p->v_right) {
  170.         p = p->v_right;
  171.         continue;
  172.     }
  173.     do {
  174.         c = p;
  175.         p = p->v_parent;
  176.     } while (p->v_right == c);
  177.     goto x;
  178.     }
  179.  
  180. }
  181.  
  182. struct varent *
  183. tw_start_shell_list()
  184. {
  185.     register struct varent *p;
  186.     register struct varent *c;
  187.  
  188.     p = &shvhed;        /* start at beginning of variable list */
  189.  
  190.     for (;;) {
  191.     while (p->v_left)
  192.         p = p->v_left;
  193. x:
  194.     if (p->v_parent == 0)    /* is it the header? */
  195.         return (NULL);
  196.     if (p->v_name)
  197.         return (p);        /* found first one */
  198.     if (p->v_right) {
  199.         p = p->v_right;
  200.         continue;
  201.     }
  202.     do {
  203.         c = p;
  204.         p = p->v_parent;
  205.     } while (p->v_right == c);
  206.     goto x;
  207.     }
  208. }
  209.  
  210. Char   *
  211. tw_next_shell_var(vptr)
  212.     struct varent **vptr;
  213. {
  214.     register struct varent *p;
  215.     register struct varent *c;
  216.     register Char *cp;
  217.  
  218.     if ((p = *vptr) == NULL)
  219.     return (NULL);        /* just in case */
  220.  
  221.     cp = p->v_name;        /* we know that this name is here now */
  222.  
  223.     /* now find the next one */
  224.     for (;;) {
  225.     if (p->v_right) {    /* if we can go right */
  226.         p = p->v_right;
  227.         while (p->v_left)
  228.         p = p->v_left;
  229.     }
  230.     else {            /* else go up */
  231.         do {
  232.         c = p;
  233.         p = p->v_parent;
  234.         } while (p->v_right == c);
  235.     }
  236.     if (p->v_parent == 0) {    /* is it the header? */
  237.         *vptr = NULL;
  238.         return (cp);
  239.     }
  240.     if (p->v_name) {
  241.         *vptr = p;        /* save state for the next call */
  242.         return (cp);
  243.     }
  244.     }
  245.  
  246. }
  247.  
  248. Char  **
  249. tw_start_env_list()
  250. {
  251.     return (STR_environ);
  252. }
  253.  
  254. Char   *
  255. Getenv(str)
  256.     Char   *str;
  257. {
  258.     Char  **var;
  259.     int     len, res;
  260.  
  261.     len = Strlen(str);
  262.     for (var = STR_environ; var != NULL && *var != NULL; var++)
  263.     if ((*var)[len] == '=') {
  264.         (*var)[len] = '\0';
  265.         res = StrQcmp(*var, str);
  266.         (*var)[len] = '=';
  267.         if (res == 0)
  268.         return (&((*var)[len + 1]));
  269.     }
  270.     return (NULL);
  271. }
  272.  
  273. Char   *
  274. tw_next_env_var(evp)
  275.     Char ***evp;
  276. {
  277.     static Char buffer[MAXVARLEN + 1];
  278.     Char   *ps, *pd;
  279.  
  280.     if (*evp == NULL || **evp == NULL)
  281.     return (NULL);
  282.     for (ps = **evp, pd = buffer;
  283.      *ps && *ps != '=' && pd <= &buffer[MAXVARLEN]; *pd++ = *ps++);
  284.     *pd = '\0';
  285.     (*evp)++;
  286.     return (buffer);
  287. }
  288.